package com.local.controller;


import com.base.controller.BaseController;
import com.base.util.PageData;
import com.base.util.Tools;
import com.local.service.CommonManager;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * Created by lee on 2016/7/11.
 * This Controller is used for User Management Surface
 */
@Controller
public class CommonController extends BaseController {

    @Resource(name = "commonService")
    private CommonManager commonManager;

    @ResponseBody
    @RequestMapping(value = "/delete",produces = "application/json;charset=UTF-8")
    public ModelAndView delete(){
        ModelAndView mv = this.getModelAndView();
        HttpSession session = this.getRequest().getSession();
        PageData user = (PageData) session.getAttribute("USER");
        PageData pd = this.getPageData();
        pd.put("C_USERID",Double.valueOf(user.get("C_USERID").toString()));
        pd.put("RPTYEAR",Tools.date2StrYear(new Date()));
        String flag = pd.getString("FLAG");
        commonManager.deleteCommon(pd,Tools.flag2Mapper(flag.toUpperCase()+"_DEL"));
        List<PageData> list = commonManager.getInfoCommon(pd,Tools.flag2Mapper(flag.toUpperCase()+"_INFS"));
        mv.addObject("list",list);
        mv.setViewName("local/ajax/"+flag.toLowerCase()+"_ajax");
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = "/updInfo",produces = "application/json;charset=UTF-8")
    public String updInfo(){
        HttpSession session = this.getRequest().getSession();
        PageData user = (PageData) session.getAttribute("USER");
        PageData pd = this.getPageData();
        pd.put("C_USERID",Double.valueOf(user.get("C_USERID").toString()));
        pd.put("RPTYEAR",Tools.date2StrYear(new Date()));
        String flag = pd.getString("FLAG");
        JSONObject getObj = new JSONObject();
        getObj.put("ByID",commonManager.getInfoCommonByID(pd,Tools.flag2Mapper(flag.toUpperCase()+"_INF")));
        return getObj.toString();
    }

    @ResponseBody
    @RequestMapping(value = "/save",produces = "application/json;charset=UTF-8")
    public ModelAndView save(){
        ModelAndView mv = this.getModelAndView();
        HttpSession session = this.getRequest().getSession();
        PageData user = (PageData) session.getAttribute("USER");
        PageData pd = this.getPageData();
        String flag = pd.getString("FLAG");
        double value = 0;
        pd.put("C_USERID",Double.valueOf(user.get("C_USERID").toString()));
        pd.put("RPTYEAR",Tools.date2StrYear(new Date()));
        pd.put("RPTDATE",Tools.str2Date(Tools.date2Str(new Date())));
        pd.put("UPDATETIME",Tools.str2Date(Tools.date2Str(new Date())));

        if(pd.get("SID")==null){
            pd.put("UIFLAG","INS");
        }else if(pd.get("SID")!=null&&pd.get("SID").toString().equals("")){
            pd.put("UIFLAG","INS");
        }else if(pd.get("SID")!=null&&!pd.get("SID").toString().equals("")){
            pd.put("UIFLAG","UPD");
        }
        pd.put("TYPES",flag);
        commonManager.insertUpdCommon(pd,Tools.flag2Mapper(flag.toUpperCase()+"_INS"));
        List<PageData> list = commonManager.getInfoCommon(pd,Tools.flag2Mapper(flag.toUpperCase()+"_INFS"));
        if(flag.equals("GUPIAO")||flag.equals("JIJIN")){
            for(PageData gp : list){
                value += Double.valueOf(gp.get("VALUEINFO").toString());
            }
        }

        mv.addObject("list",list);
        mv.addObject("value",value);
        mv.setViewName("local/ajax/"+flag.toLowerCase()+"_ajax");
        return mv;
    }
    //单条信息，不需要异步刷新div

    @ResponseBody
    @RequestMapping(value = "/singleInfoSave",produces = "application/json;charset=UTF-8")
    public void singleInfoSave(){
        PageData pd = this.getPageData();
        HttpSession session = this.getRequest().getSession();
        PageData user = (PageData) session.getAttribute("USER");
        pd.put("C_USERID",Integer.valueOf(user.get("C_USERID").toString()));
        pd.put("RPTYEAR",Tools.date2StrYear(new Date()));
        pd.put("RPTDATE",Tools.str2Date(Tools.date2Str(new Date())));
        pd.put("UPDATETIME",Tools.str2Date(Tools.date2Str(new Date())));

        String flag = pd.getString("FLAG");
        PageData info = commonManager.getInfoCommonByID(pd,Tools.flag2Mapper(flag+"_INF"));
        if(info==null){
            pd.put("UIFLAG","INS");
        }else{
            pd.put("UIFLAG","UPD");
            pd.put("PRIMARYSID",info.get("SID").toString());
        }
        commonManager.insertUpdCommon(pd,Tools.flag2Mapper(flag+"_INS"));
    }

}
